<?php defined('BASEPATH') or exit('No direct script access allowed');

class Offline_payment_m extends MY_Model {	
	function __construct(){
		parent::__construct();
	}
	
	function add_coin_offline(){
		$array_error = array();
		$USER = $this->user_entity->init('id', getUserId());
		
		if($_POST['type'] == 'bank_deposit'){
			if( $this->phpvalidator->is_empty($this->input->post('bank_name','')) ){
				$array_error[] = 'bank_name_error';
			}
			if( $this->phpvalidator->is_empty($this->input->post('bank_branch','')) ){
				$array_error[] = 'bank_branch_error';
			}
			$deposited_date =  convertToTimeStampFromFormat($_POST['deposited_date'], 'd/m/Y');
			if($deposited_date == 0 ){
				$array_error[] = 'deposited_date_error';
			}
			if(!empty($array_error)){
				echo 'err#'.implode('#',$array_error);
				exit();
			}
			$newId = $this->offline_payment_entity->createNewEntity();
			$OFFLINE_PAYMENT = $this->offline_payment_entity->init('id',$newId);
			
			$attachment = '';
			if(isset($_FILES["attachment"]) AND !empty($_FILES["attachment"]['name'])) {
				if(($_FILES["attachment"]['size']/1000000) > allowMaxFileSize() ){
					$attachment = '';
				}else{
					$attachment = $this->module_helper->uploadFile( 
											$_FILES["attachment"]['tmp_name'],
											$_FILES["attachment"]['name'],
											$USER->getFolderFileStorage(),
											allowExtensionFileDocumentUpload()
										);
				}	
			}
			$PACKAGE = $this->coin_packages_entity->init('id', $_POST['package']);	
			$OFFLINE_PAYMENT->setUserId($USER->getId());
			$OFFLINE_PAYMENT->setPackageId($_POST['package']);
			$OFFLINE_PAYMENT->setCoin($PACKAGE->getCoin());
			$OFFLINE_PAYMENT->setStatus('pending');
			$OFFLINE_PAYMENT->setPaymentType('bank_deposit');
			$OFFLINE_PAYMENT->setAttachment($attachment);
			$OFFLINE_PAYMENT->setBankName($this->input->post('bank_name',''));
			$OFFLINE_PAYMENT->setBankBranch($this->input->post('bank_branch',''));
			$OFFLINE_PAYMENT->setDepositedDate($deposited_date);
			
			$OFFLINE_PAYMENT->update();
			echo 'ok';
			exit();
		}
		
		if($_POST['type'] == 'neft_transfer'){
			if( $this->phpvalidator->is_empty($this->input->post('remitter_ac_name','')) ){
				$array_error[] = 'remitter_ac_number_error';
			}
			if( $this->phpvalidator->is_empty($this->input->post('remitter_ac_number','')) ){
				$array_error[] = 'remitter_ac_number_error';
			}
			if( $this->phpvalidator->is_empty($this->input->post('remitter_bank_name','')) ){
				$array_error[] = 'remitter_bank_name_error';
			}
			if( $this->phpvalidator->is_empty($this->input->post('transfer_time','')) ){
				$array_error[] = 'transfer_time_error';
			}
			if( $this->phpvalidator->is_empty($this->input->post('transaction_id','')) ){
				$array_error[] = 'transaction_id_error';
			}
			$transfer_date =  convertToTimeStampFromFormat($_POST['transfer_date'], 'd/m/Y');
			if($transfer_date == 0 ){
				$array_error[] = 'transfer_date_error';
			}
			if(!empty($array_error)){
				echo 'err#'.implode('#',$array_error);
				exit();
			}
			$newId = $this->offline_payment_entity->createNewEntity();
			$OFFLINE_PAYMENT = $this->offline_payment_entity->init('id',$newId);
			
			$attachment = '';
			if(isset($_FILES["attachment"]) AND !empty($_FILES["attachment"]['name'])) {
				if(($_FILES["attachment"]['size']/1000000) > allowMaxFileSize() ){
					$attachment = '';
				}else{
					$attachment = $this->module_helper->uploadFile( 
											$_FILES["attachment"]['tmp_name'],
											$_FILES["attachment"]['name'],
											$USER->getFolderFileStorage(),
											allowExtensionFileDocumentUpload()
										);
				}	
			}
			$PACKAGE = $this->coin_packages_entity->init('id', $_POST['package']);	
			$OFFLINE_PAYMENT->setUserId($USER->getId());
			$OFFLINE_PAYMENT->setPackageId($_POST['package']);
			$OFFLINE_PAYMENT->setCoin($PACKAGE->getCoin());
			$OFFLINE_PAYMENT->setStatus('pending');
			$OFFLINE_PAYMENT->setPaymentType('neft_transfer');
			$OFFLINE_PAYMENT->setAttachment($attachment);
			$OFFLINE_PAYMENT->setRemitterAcName($this->input->post('remitter_ac_name',''));
			$OFFLINE_PAYMENT->setRemitterAcNumber($this->input->post('remitter_ac_number',''));
			$OFFLINE_PAYMENT->setRemitterBankName($this->input->post('remitter_bank_name',''));
			$OFFLINE_PAYMENT->setTransferDate($transfer_date);
			$OFFLINE_PAYMENT->setTransferTime($this->input->post('transfer_time',''));
			$OFFLINE_PAYMENT->setTransactionId($this->input->post('transaction_id',''));
			
			$OFFLINE_PAYMENT->update();
			echo 'ok';
			exit();
		}
		
		if($_POST['type'] == 'dd'){
			if( $this->phpvalidator->is_empty($this->input->post('dd_no','')) ){
				$array_error[] = 'dd_no_error';
			}
			if( $this->phpvalidator->is_empty($this->input->post('dd_bank_name','')) ){
				$array_error[] = 'dd_bank_name_error';
			}
			if( $this->phpvalidator->is_empty($this->input->post('remitter_address','')) ){
				$array_error[] = 'remitter_address_error';
			}
			
			$dd_date =  convertToTimeStampFromFormat($_POST['dd_date'], 'd/m/Y');
			if($dd_date == 0 ){
				$array_error[] = 'dd_date_error';
			}
			if(!empty($array_error)){
				echo 'err#'.implode('#',$array_error);
				exit();
			}
			$newId = $this->offline_payment_entity->createNewEntity();
			$OFFLINE_PAYMENT = $this->offline_payment_entity->init('id',$newId);
			
			$attachment = '';
			if(isset($_FILES["attachment"]) AND !empty($_FILES["attachment"]['name'])) {
				if(($_FILES["attachment"]['size']/1000000) > allowMaxFileSize() ){
					$attachment = '';
				}else{
					$attachment = $this->module_helper->uploadFile( 
											$_FILES["attachment"]['tmp_name'],
											$_FILES["attachment"]['name'],
											$USER->getFolderFileStorage(),
											allowExtensionFileDocumentUpload()
										);
				}	
			}
			$PACKAGE = $this->coin_packages_entity->init('id', $_POST['package']);	
			$OFFLINE_PAYMENT->setUserId($USER->getId());
			$OFFLINE_PAYMENT->setPackageId($_POST['package']);
			$OFFLINE_PAYMENT->setCoin($PACKAGE->getCoin());
			$OFFLINE_PAYMENT->setStatus('pending');
			$OFFLINE_PAYMENT->setPaymentType('dd');
			$OFFLINE_PAYMENT->setAttachment($attachment);
			$OFFLINE_PAYMENT->setDdNo($this->input->post('dd_no',''));
			$OFFLINE_PAYMENT->setDdBankName($this->input->post('dd_bank_name',''));
			$OFFLINE_PAYMENT->setRemitterAddress($this->input->post('remitter_address',''));
			$OFFLINE_PAYMENT->setDdDate($dd_date);
			
			$OFFLINE_PAYMENT->update();
			echo 'ok';
			exit();
		}
		
		if($_POST['type'] == 'emoney_order'){
			if( $this->phpvalidator->is_empty($this->input->post('emoney_order_no','')) ){
				$array_error[] = 'emoney_order_no_error';
			}
			if( $this->phpvalidator->is_empty($this->input->post('emoney_remitter_address','')) ){
				$array_error[] = 'emoney_remitter_address_error';
			}
			
			$emoney_order_date =  convertToTimeStampFromFormat($_POST['emoney_order_date'], 'd/m/Y');
			if($emoney_order_date == 0 ){
				$array_error[] = 'emoney_order_date_error';
			}
			if(!empty($array_error)){
				echo 'err#'.implode('#',$array_error);
				exit();
			}
			$newId = $this->offline_payment_entity->createNewEntity();
			$OFFLINE_PAYMENT = $this->offline_payment_entity->init('id',$newId);
			
			$attachment = '';
			if(isset($_FILES["attachment"]) AND !empty($_FILES["attachment"]['name'])) {
				if(($_FILES["attachment"]['size']/1000000) > allowMaxFileSize() ){
					$attachment = '';
				}else{
					$attachment = $this->module_helper->uploadFile( 
											$_FILES["attachment"]['tmp_name'],
											$_FILES["attachment"]['name'],
											$USER->getFolderFileStorage(),
											allowExtensionFileDocumentUpload()
										);
				}	
			}
			$PACKAGE = $this->coin_packages_entity->init('id', $_POST['package']);	
			$OFFLINE_PAYMENT->setUserId($USER->getId());
			$OFFLINE_PAYMENT->setPackageId($_POST['package']);
			$OFFLINE_PAYMENT->setCoin($PACKAGE->getCoin());
			$OFFLINE_PAYMENT->setStatus('pending');
			$OFFLINE_PAYMENT->setPaymentType('emoney_order');
			$OFFLINE_PAYMENT->setAttachment($attachment);
			$OFFLINE_PAYMENT->setEmoneyOrderNo($this->input->post('emoney_order_no',''));
			$OFFLINE_PAYMENT->setEmoneyRemitterAddress($this->input->post('emoney_remitter_address',''));
			$OFFLINE_PAYMENT->setEmoneyOrderDate($emoney_order_date);
			
			$OFFLINE_PAYMENT->update();
			echo 'ok';
			exit();
		}
	}	
	
// end class
}	